<?php

namespace app\admin\app\model;

use GuzzleHttp\Client;
use plugin\admin\app\model\Base;

/**
 * @property integer $id 主键(主键)
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $name 名称
 * @property integer $status 状态
 * @property integer $admin_id 管理员id
 */
class Category extends Base
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_category';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';


    public function getAITitle()
    {
        $apiUrl = 'https://api.deepseek.com/v1/chat/completions';
        $apiKey = 'sk-560cfe79401841b289f6b58479bf1014';

        // 构建请求数据
        $requestData = [
            'model' => 'deepseek-chat',
            'messages' => [
                [
                    'role' => 'user',
                    'content' => "请提供一些抖音{$this->name}分类的热门标题，只返回标题就行"
                ]
            ]
        ];
        // 创建 Guzzle 客户端
        $client = new Client();
        try {
            // 发送 POST 请求
            $response = $client->post($apiUrl, [
                'headers' => [
                    'Authorization' => 'Bearer ' . $apiKey,
                    'Content-Type' => 'application/json'
                ],
                'json' => $requestData
            ]);

            // 获取响应内容
            $responseBody = json_decode($response->getBody(), true);
            $titles = explode("\n", $responseBody['choices'][0]['message']['content']);
            $rand = array_rand($titles);
            return ['code' => 1, 'title' => trim(strstr($titles[$rand], '.'), " \n\r\t\v\0.")];
        } catch (\Exception $e) {
            return ['code' => 0, 'title' => $e->getMessage()];
        }
    }
}
